package com.yh.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


//定义一个工具类，用来获取当前登录的用户



import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.yh.bean.User;

public class UserContextUtil {

	//定义一个常量，用来保存登录用户
	public static final String LOGIN_USER = "loginUser";

	public static void setUser(User user,HttpSession session){
		session.setAttribute(LOGIN_USER, user);
	}
	
	public static User getUser(HttpSession session){
		return (User) session.getAttribute(LOGIN_USER);
	}
	
	public static HttpServletRequest getRequest(){
		ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = requestAttributes.getRequest();
	
		return request;
	}
	
	public static HttpSession getSession(){
		
		return getRequest().getSession();
	}
	
	public static User getUser(){
		
		
		return (User) getSession().getAttribute(LOGIN_USER);
	}
}
